<?php

namespace App\Http\Controllers\SubAccount;

use App\Common\Traits\Model\DepartmentSearchTrait;
use App\Common\Traits\Model\ModelSearchTrait;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use App\Models\SubAccount\SubAccountStaffBind;
use Illuminate\Http\Request;
use App\Helpers\SubAccountTrait;

class SubAccountLoginLogoutController extends Controller
{
    use ModelSearchTrait, DepartmentSearchTrait, SubAccountTrait, ServiceTrait;

    //
    public function index(Request $request, SubAccountLoginLogoutRecord $loginLogoutRecord){
        $loginLogoutRecord=$this->singleModelSearch($request, $loginLogoutRecord, $this->recordCondition());
        $loginLogoutRecord=$this->departmentSearch($request,$loginLogoutRecord);
        $records=$loginLogoutRecord->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        return $this->success($records);
    }
    //搜索条件
    public function recordCondition(){
        return [
            ['sub_account','like'],
            ['staff_name','like'],
            ['login_time','between'],
            ['logout_time','between'],
            ['is_data_recorded','='],
        ];
    }
    //
    public function staffLoginList(Request $request, SubAccountLoginLogoutRecord $loginLogoutRecord){
        $loginLogoutRecord=$this->singleModelSearch($request, $loginLogoutRecord, $this->recordCondition());
        $loginLogoutRecord=$this->departmentSearch($request,$loginLogoutRecord);
        $user=auth()->user();
        $loginLogoutRecord=$this->model($loginLogoutRecord,$user);
        $dateWhere=date('Y-m-d');
        if($request->date=='today'){
            $loginLogoutRecord=$this->setShiftWhere($user,$loginLogoutRecord);
            $loginLogoutRecord=$loginLogoutRecord->where('log_in_out_date',$dateWhere);
        }else{
            $loginLogoutRecord=$loginLogoutRecord->where('log_in_out_date','<',$dateWhere);
        }

        if($request->filled('page')){
            $records=$loginLogoutRecord->orderByDesc('updated_at')->paginate($request->size??20)->withQueryString();
        }else{
            $records=$loginLogoutRecord->orderByDesc('updated_at')->get();
        }

        return $this->success($records);
    }

    //
    public function subAccountLogin(Request $request){
        if(!$request->filled('login_certificate')){
            return $this->failed('上号凭证不能为空',201);
        }
        $record=SubAccountLoginLogoutRecord::find($request->record_id??0);
        if(empty($record)){
            return $this->failed('记录id错误',201);
        }
        if($record->log_in_out_date!=date('Y-m-d')){
            return $this->failed('历史记录不可再进行上号操作',201);
        }
        if(empty($record->account)){
            return $this->failed('账号不存在',201);
        }
        if($record->account->status==0){
            return $this->failed('此子账号已停用，不能再上',201);
        }
        if(!$this->checkAccountStaff($record,$request->user())){
            return $this->failed('只能上分配给自己的号',201);
        }
        $res=$this->updateLoginData($record,$request);
        return $this->success(['msg'=>'上号成功']);
    }
    //验证是否是该客服的号
    public function checkAccountStaff($record,$user){
        $count=SubAccountStaffBind::where('sub_account_id',$record->sub_account_id)->where('staff_id',$user->id??0)->count('id');
        if($count){
            return true;
        }
        return false;
    }
    //
    public function updateLoginData($record,$request){
        $record->staff_id=auth()->user()->id??'';
        $record->staff_name=auth()->user()->name??'';
        $record->department3=auth()->user()->department->name??'';
        $record->login_time=now();
        $record->login_certificate=$request->login_certificate;
        $record->exception_img=$request->exception_img??null;
        return $record->save();
    }

    //
    public function subAccountLogout(Request $request){
        if(!$request->filled('logout_log')){
            return $this->failed('交班日志不能为空',201);
        }
        $record=SubAccountLoginLogoutRecord::find($request->record_id??0);
        if(empty($record)){
            return $this->failed('记录id错误',201);
        }
        if($record->staff_id!=auth()->user()->id){
            return $this->failed('只能交自己上的号的班',201);
        }
        $res=$this->updateLogoutData($record,$request);
        return $this->success(['msg'=>'交班成功']);
    }
    //
    public function updateLogoutData($record,$request){
        $record->logout_time=now();
        $record->logout_log=$request->logout_log;
        return $record->save();
    }

    //获取某天上下号记录
    public function getDateLoginRecord(Request $request){
        $records=SubAccountLoginLogoutRecord::where('record_code',$request->record_code??0)->get();
        return $this->success($records);
    }

    //某个子账号昨天的记录
    public function yesterdayLogs(Request $request, SubAccountLoginLogoutRecord $loginLogoutRecord){
        if(!$request->filled('log_in_out_date')){
            return $this->failed('记录日期不能为空',201);
        }
        $logs=$loginLogoutRecord->yesterdayLogs($request->log_in_out_date,$request->sub_account_id);
        return $this->success($logs);
    }

}
